import Mock from 'mockjs'

Mock.mock(window.location.host+'/getProjects', function (req, res, next) {
   // console.log(req)
    let data = Mock.mock({
        "list|100": [
            {
                projectId: "@guid()",
                projectName: "@city()"+'项目组',
                name: "@cname()"+'合同',
                code: 'HT'+"@id()",
                dep: "@city()"+'部',
                money:'@float(10000,1000000)',
                workload: "@integer(1,1000)",
            },
        ],
    });
    data.list =uniqueByKey(data.list,'projectName')
    for(let i=0;i<data.list.length;i++){
        let currTime1 = new Date();
        let currTime2 = new Date();
        currTime1.setDate(currTime1.getDate() - (Math.floor(Math.random() * (100 - 1 + 1)) + 1)); 
        currTime2.setDate(currTime2.getDate() + (Math.floor(Math.random() * (1000 - 1 + 1)) + 1)); 
        data.list[i].serviceStartTime = timestampToTime(currTime1,'YMD')
        data.list[i].serviceEndTime = timestampToTime(currTime2,'YMD')
        data.list[i].money = Number(data.list[i].money).toFixed(2)
        data.list[i].workload = parseInt(data.list[i].workload)
    }
    return {
        code:200,
        data:data.list
    }
  }
);

Mock.mock(window.location.host+'/info/library/list', function (req, res, next) {
    //console.log()
    let proList = JSON.parse(req.body).data
    let pageSize = JSON.parse(req.body).pageSize
    let data = []
    for(let i=0;i<pageSize;i++){
        let obj = Mock.mock({
                    orderCode: 'DD'+"@id()",
                    orderServiceTerm:"@integer(1,5)",
                    orderWorkload:"@integer(1,10000)",
                    orderMoney:'@float(10000,1000000)',
                    budget:'@cparagraph()',
                    status:"@integer(0,2)",
        });
        data = data.concat(obj)
    }
    for(let i=0;i<data.length;i++){
        let k = Math.floor(Math.random() * proList.length) 
        data[i].orderDiscount = Number( data[i].orderDiscount).toFixed(2)
        data[i].orderServiceTerm = parseInt( data[i].orderServiceTerm)
        data[i].orderWorkload = parseInt( data[i].orderWorkload)
        data[i].orderMoney =  Number( data[i].orderMoney).toFixed(2)
        data[i].orderDiscount = Number(Math.random() * (1 - 0.6) + 0.6).toFixed(2)
        data[i].orderName = data[i].orderCode+'-'+ proList[k].name
        data[i].status  = parseInt( data[i].status)
        data[i].addTime = new Date().getTime()
        data[i] = Object.assign({},proList[k], data[i])
    }
    return {
        code:200,
        data:data
    }
  }
);
Mock.mock(window.location.host+'/info/library/add', function (req, res, next) {
    let pro = JSON.parse(req.body).data
    let data = []
    for(let i=0;i<10;i++){
        let obj = Mock.mock({
            orderCode: 'DD'+"@id()",
            orderServiceTerm:"@integer(1,5)",
            orderWorkload:"@integer(1,10000)",
            orderMoney:'@float(10000,1000000)',
            budget:'@cparagraph()',
            status:"@integer(0,2)",
        });
        data = data.concat(obj)
    }
    for(let i=0;i<data.length;i++){
        data[i].orderDiscount = Number( data[i].orderDiscount).toFixed(2)
        data[i].orderServiceTerm = parseInt( data[i].orderServiceTerm)
        data[i].orderWorkload = parseInt( data[i].orderWorkload)
        data[i].orderMoney =  Number( data[i].orderMoney).toFixed(2)
        data[i].orderDiscount = Number(Math.random() * (1 - 0.6) + 0.6).toFixed(2)
        data[i].orderName = data[i].orderCode+'-'+ pro.name
        data[i].status  = parseInt( data[i].status)
        data[i].addTime = new Date().getTime()
        data[i] = Object.assign({},pro, data[i])
    }
    return {
        code:200,
        data:data
    }
  }
);
function uniqueByKey(arr, key) {
    const map = arr.reduce((acc, obj) => {
      const keyValue = obj[key];
      acc[keyValue] = obj;
      return acc;
    }, {});
    return Object.values(map);
  }
// 格式化日期
function timestampToTime( timeStamp, type, middleStr = '-' ) {
    let date = timeStamp ? new Date( timeStamp ) : new Date()
    let Y = date.getFullYear() + middleStr
    let M =
      ( date.getMonth() + 1 < 10 ?
        '0' + ( date.getMonth() + 1 ) :
        date.getMonth() + 1 )
    let D = date.getDate() < 10 ? middleStr + '0' + date.getDate() : middleStr + date.getDate() + ''
    let h = ( date.getHours() < 10 ? '0' + date.getHours() : date.getHours() ) + ':'
    let m =
      ( date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes() )
    let s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
    s = ':' + s
    if ( type === 'YMD' ) {
      return Y + M + D
    } else if ( type === 'YM' ) {
      return Y + M
    } else if ( type === 'YMDHM' ) {
      return Y + M + D + ' ' + h + m
    } else {
      return Y + M + D + ' ' + h + m + s
    }
  }
  